#include<bits/extc++.h>
#include<bits/stdc++.h>
using namespace std;

#define OFF 32

typedef long long ll;
typedef __gnu_pbds::tree<ll,__gnu_pbds::null_type,less<ll>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> set_t;

template<typename T>
inline void read(T &x){
    x=0;T f=1;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');
    x*=f;
}

int main(){
  #ifdef BUG
  freopen("a.in","r",stdin);
  #endif
  set_t st;
  ll n,a,b,c;
  read(n);
  unordered_map<ll,ll> um(n<<1);
  while(n--){
    read(a);read(b);
    if(a==1){
      ++um[b];
      b=um[b]+(b<<OFF);
      st.insert(b);
    }else if(a==2){
      c=(b<<OFF)+um[b];
      st.erase(c);
      --um[b];
    }else if(a==3){
      printf("%d\n",st.order_of_key(b<<OFF)+1);
    }else if(a==4){
      printf("%d\n",(*st.find_by_order(b-1))>>OFF);
    }else if(a==5){
      auto it=st.lower_bound(b<<OFF);
      --it;
      printf("%d\n",(*it)>>OFF);
    }else{
      c=(b<<OFF);
      if(um[b])c+=um[b];
      auto it=st.upper_bound(c);
      printf("%d\n",((*it)>>OFF));
    }
  }
}
